/**
 * app edit
 */
let _this;
layui.use(['form'], function () {
    let form = layui.form,
        layer = layui.layer,
        $ = layui.$;
    let u = localStorage.getItem("a@m");
    let loginU = {};
    if (u == undefined) {
        // layer.msg('请登录', {icon: 1, time: 500}, function () {
        //     window.location = 'page/login.html';
        // });
    } else {
        loginU = decryptLocalStorage(u);
    }
    let _data = localStorage.getItem("appData");
    let appData = undefined;
    if (_data != undefined) {
        appData = decryptLocalStorage(_data, loginU.token);
        $("#appId").val(appData.appId);
        $("#appName").val(appData.appName);
        $("#newAppUrl").val(appData.newAppUrl);
        $("#newAppLogo").val(appData.newAppLogo);
        $("#newAppSize").val(appData.newAppSize);
        $("#appPackageName").val(appData.appPackageName);
        $("#newAppVersionCode").val(appData.newAppVersionCode);
        $("#newAppVersionName").val(appData.newAppVersionName);
        $("#newAppUpdateDesc").val(appData.newAppUpdateDesc);
        if (_this != undefined) {
            _this.appName = appData.appName;
            _this.newAppUrl = appData.newAppUrl;
            _this.newAppLogo = appData.newAppLogo;
            _this.newAppSize = appData.newAppSize;
            _this.newAppVersionCode = appData.newAppVersionCode;
            _this.newAppVersionName = appData.newAppVersionName;
            _this.newAppUpdateDesc = appData.newAppUpdateDesc;
            _this.appLogoUrl = "/" + appData.newAppLogo;
        }
    }
    //监听提交
    form.on('submit(saveBtn)', function (data) {
        var params = data.field;
        params.token = loginU.token;
        //添加
        if (appData == undefined) {
            request.add('app', JSON.stringify(params), function (result) {
                if (result.code == 0) {
                    layer.msg('保存成功', {
                        icon: 1,
                        time: 500
                    }, function () {
                        // 关闭弹出层
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });

                } else {
                    layer.msg('保存失败', {
                        icon: 2,
                        time: 2000
                    });
                }
            });
        }
        //修改
        else {
            params.appId = appData.appId;
            request.update('app', JSON.stringify(params), function (result) {
                if (result.code == 0) {
                    layer.msg('保存成功', {
                        icon: 1,
                        time: 500
                    }, function () {
                        // 关闭弹出层
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });

                } else {
                    layer.msg('保存失败', {
                        icon: 2,
                        time: 2000
                    });
                }
            });
        }
        return false;
    });
});
new Vue({
    el: '#appContainVue',
    data: {
        appLogoUrl: '',
        newAppLogo: '',
        newAppUrl: '',
        newAppSize: 0,
        appName: '',
        newAppVersionName: '',
        newAppVersionCode: '',
        appPackageName: '',
        uploadAppUrl: baseUrl + "admin/v1/common/uploadApk"
    },
    mounted() {
        _this = this;
        let u = localStorage.getItem("a@m");
        let loginU = decryptLocalStorage(u);
        let _url = this.uploadAppUrl;
        this.uploadAppUrl = _url + "?token=" + loginU.token;
    },
    methods:
        {
            handleAvatarSuccess(res, file) {
                this.imageUrl = URL.createObjectURL(file.raw);
                if (res.data != undefined) {
                    //appLogo,appPath,appLabel,appVersion,appCode
                    this.newAppLogo = res.data[0];
                    this.appLogoUrl = baseUrl + "admin/v1/common/showImageByServer?filePath=" + res.data[0];
                    this.newAppUrl = res.data[1];
                    this.appName = res.data[2];
                    this.newAppVersionName = res.data[3];
                    this.newAppVersionCode = res.data[4];
                    this.appPackageName = res.data[5];
                }
            },
            beforeAvatarUpload(file) {
                this.newAppSize = fileLengthFormat(file.size, 1);
                return true;
            }
        }
});
